<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
    <script src="./utils/createDraw.js"></script>
    <script src="./utils/Query.js"></script>
    <script src="./utils/delFeatures.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #btn {
            width: 200px;
            height: 50px;
            position: fixed;
            z-index: 1000;
            top: 20px;
            left: 50px;

        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <button id="btn" onclick="activeDraw()">激活画笔</button>
    <div id="map">
    </div>
    <script>
        var docLayer = new Zondy.Map.Doc("", "myTestMap1", {

        })
        var map = new ol.Map({
            target: "map",
            layers: [gaode, docLayer],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 4
            })
        })
        /* 1、设置画笔 geometry */
        let source = new ol.source.Vector({})
        var layer = new ol.layer.Vector({
            source
        })
        map.addLayer(layer);
        // 出现内部报错，最大的可能是service里边的name或图层索引layerId填错了。
        let service = {
            name: "myTestMap1",
            layerId: 4
        }
        function activeDraw() {
            let draw = createDraw({
                // type: "Point", // 画完后getGeometry。geometry.getType()得到 'Point'
                type: "Circle",// 'Circle'
                // type: "Square",// 'Polygon'
                // type: "LineString",// 'LineString'
                // type: "Box",// 'Polygon'
                // type: "Polygon",// 'Polygon'
                /* type: Point,Circle,Square,LineString,Box,Polygon
   Square正方形(Circle的一种) Box矩形(LineString的一种) */
                source
            })
            map.addInteraction(draw);
            draw.on("drawend", evt => {
                source.clear();
                var geometry = evt.feature.getGeometry();
                // console.log(geometry,'geometry')
                Query.queryByGeom({
                    geometry,
                    service,
                    success,
                })
            })
        }
        /* 2、Query.queryByGeom 获取要素 fids */

        function success(features) {
            console.log(features, 'features')
            // var fids = data.SFEleArray.map(item => item.FID);
            if (features) {
                const fids = features.map(item => item.id_)
                console.log(fids, 'features_fids')
                /* 3、调用删除的方法 delFeature */
                delFeatures({
                    fids,
                    service,
                    docLayer
                })
            }else{
                alert('您没有选中要删除的要素')
            }

        }

    </script>
</body>

</html>